package com.tyt.lelan.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

import com.tyt.lelan.provider.FeedsData.AllFeedsColumns;
import com.tyt.lelan.provider.FeedsData.CategoryColumns;
import com.tyt.lelan.provider.FeedsData.EntryColumns;
import com.tyt.lelan.provider.FeedsData.IconColumns;
import com.tyt.lelan.provider.FeedsData.MyFeedsColumns;

public class DataContentProvider extends ContentProvider{
	
	private DatabaseHelper mOpenHelper;
	
	private static UriMatcher sUriMatcher=null;
	private static final int URI_MYFEEDS = 0;
	private static final int URI_CATEGORY=1;
	private static final int URI_ALLFEEDS=2;
	private static final int URI_ICONS=3;
	private static final int URI_ENTRIES = 4;
	
	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(FeedsData.AUTHORITY, MyFeedsColumns.TABLENAME, URI_MYFEEDS);
		sUriMatcher.addURI(FeedsData.AUTHORITY, CategoryColumns.TABLENAME, URI_CATEGORY);
		sUriMatcher.addURI(FeedsData.AUTHORITY, AllFeedsColumns.TABLENAME, URI_ALLFEEDS);
		sUriMatcher.addURI(FeedsData.AUTHORITY, IconColumns.TABLENAME, URI_ICONS);
		sUriMatcher.addURI(FeedsData.AUTHORITY, EntryColumns.TABLENAME, URI_ENTRIES);
	}
	
	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		mOpenHelper = new DatabaseHelper(getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		switch(sUriMatcher.match(uri)){
		case URI_MYFEEDS:
			qb.setTables(MyFeedsColumns.TABLENAME);
			break;
		case URI_CATEGORY:
			qb.setTables(CategoryColumns.TABLENAME);
			break;		
		case URI_ALLFEEDS:
			qb.setTables(AllFeedsColumns.TABLENAME);
			break;
		case URI_ICONS:
			qb.setTables(IconColumns.TABLENAME);
			break;
		case URI_ENTRIES : {
			qb.setTables(EntryColumns.TABLENAME);
			break;
		}
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		
		// Get the database and run the query
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, null);
		
		// Tell the cursor what uri to watch, so it knows when its source data changes
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	@Override
	/**
	 * Return the MIME type of the data at the given URI. 
	 * This should start with vnd.android.cursor.item for a single record,
	 * or vnd.android.cursor.dir/ for multiple items. 
	 * This method can be called from multiple threads,
     * as described in Application Fundamentals: Processes and Threads.
	 */
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		int option = sUriMatcher.match(uri);
		switch(option) {
		case URI_MYFEEDS : return MyFeedsColumns.CONTENT_TYPE_MYFEEDS;
		case URI_CATEGORY:return CategoryColumns.CONTENT_TYPE;
		case URI_ALLFEEDS:return AllFeedsColumns.CONTENT_TYPE_ALLFEEDS;
		case URI_ICONS:return IconColumns.CONTENT_TYPE_ICONS;
		default : throw new IllegalArgumentException("Unknown URI: "+uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		// TODO Auto-generated method stub
		
		String tablename="";
		Uri    contentUri = null ;
		int matchCode=sUriMatcher.match(uri);
		switch (matchCode){
		case URI_MYFEEDS:
			tablename=MyFeedsColumns.TABLENAME;
			contentUri=MyFeedsColumns.CONTENT_URI;
			break;
		case URI_CATEGORY:
			tablename=CategoryColumns.TABLENAME;
			contentUri=CategoryColumns.CONTENT_URI;
			break;
		case URI_ALLFEEDS:
			tablename=AllFeedsColumns.TABLENAME;
			contentUri=AllFeedsColumns.CONTENT_URI;
			break;
		case URI_ICONS:
			tablename=IconColumns.TABLENAME;
			contentUri=IconColumns.CONTENT_URI;
			break;
		case URI_ENTRIES:
			tablename=EntryColumns.TABLENAME;
			contentUri=EntryColumns.CONTENT_URI;
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
			
		
		ContentValues values;
		if (initialValues != null) {
			values = new ContentValues(initialValues);
		} else {
			values = new ContentValues();
		}
		
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		long rowId = db.insert(tablename, "空", values);
		db.close();
		
		if (rowId > 0) {
			Uri noteUri = ContentUris.withAppendedId(contentUri, rowId);
			getContext().getContentResolver().notifyChange(noteUri, null);
			
			return noteUri;
		}

		throw new SQLException("Failed to insert row into " + uri);
		
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int count;
		switch (sUriMatcher.match(uri)) {
		case URI_MYFEEDS:
			count = db.delete(MyFeedsColumns.TABLENAME, selection, selectionArgs);
			break;
		case URI_ENTRIES:
			count = db.delete(EntryColumns.TABLENAME, selection, selectionArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getReadableDatabase();
		int count;
		switch (sUriMatcher.match(uri)) {
		case URI_MYFEEDS:
			count = db.update(MyFeedsColumns.TABLENAME, values, selection, selectionArgs);
			break;
			
		case URI_CATEGORY:
			count = db.update(CategoryColumns.TABLENAME, values, selection, selectionArgs);
			break;
		
		case URI_ALLFEEDS:
			count = db.update(AllFeedsColumns.TABLENAME, values, selection, selectionArgs);
			break;
			
		case URI_ICONS:
			count=db.update(IconColumns.TABLENAME, values, selection, selectionArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

}
